package com.yanqu.road.server.cmd.union.cross;

import com.yanqu.road.entity.union.cross.CrossUnionInfo;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.union.cross.CrossUnionProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.config.UnionConfigMgr;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;

@Cmd(code = Protocol.S_UNION_UPGRADE_CROSS, desc = "商会晋升跨服")
public class UnionUpgradeCrossCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

        if(StringUtils.isNullOrEmpty(player.getUserInfo().getUnionUid())){
            ret = GameErrorCode.E_UNION_NO_JOIN_UNION;
            return;
        }

        CrossUnionInfo crossUnionInfo = CrossUnionMgr.getUnionInfo(player.getUserInfo().getUnionUid());
        if(crossUnionInfo == null){
            ret = GameErrorCode.E_UNION_NO_JOIN_UNION;
            return;
        }

//        int unionLevel = UnionConfigMgr.getUnionLv(crossUnionInfo.getExperience());
//        if(GameConfig.UNION_UP_LIMIT > unionLevel){
//            ret = GameErrorCode.E_UNION_LEVEL_NO_ENOUGH;
//            return;
//        }

        Property consume = new Property();
        consume.addProperty(GameConfig.GAME_MONEY_INGOTS, BigInteger.valueOf(GameConfig.UNION_UP_COST));
        if(!player.getModule(CurrencyModule.class).currencyIsEnough(consume)){
            ret = GameErrorCode.E_BAG_INGOTS_NO_ENOUGH;
            return;
        }

        player.getModule(PreReduceModule.class).preReduce(consume);

        player.sendPacket(Protocol.C_UNION_UPGRADE_CROSS, CrossUnionProto.UnionUpgradeCrossReqMsg.newBuilder());

    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            player.sendPacket(Protocol.U_UNION_UPGRADE_CROSS, CrossUnionProto.UnionUpgradeCrossRespMsg.newBuilder().setRet(ret));
        }
    }
}
